import os

from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection

from django.conf import settings


class SaaSDevelApiUploadCreate(View):
    @error_message_detection
    def post(self, request):
        # 获取文件
        file_data = request.FILES['file']
        # 生成文件名
        filename = file_data.name
        # 构建文件存储路径
        file_path = os.path.join(settings.MEDIA_ROOT, filename)
        # 将文件内容写入到指定的文件中
        with open(file_path, 'wb') as f:
            for chunk in file_data.chunks():
                f.write(chunk)
        # 构建文件的访问链接
        file_url = settings.MEDIA_URL + 'SaaSFileUpload/' + filename
        # 返回包含文件链接的响应
        return create_response(code=200, msg="", data={'file_url': file_url})
